package com.example.administrator.tenyuanpackage.home.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.tenyuanpackage.R;
import com.example.administrator.tenyuanpackage.home.bean.ResultsGoodsBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zwq on 2017/2/27.
 */

public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapter.MyViewHolder> {
    private Context context;
    private OnItemClickListener listener;
    private LayoutInflater inflater;
    private List<ResultsGoodsBean.RowsBean> data;

    public interface OnItemClickListener {
        void onItemClick(View view,int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public HomeRecyclerAdapter(Context context, List<ResultsGoodsBean.RowsBean> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_homerecycler, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Glide.with(context).load(data.get(position).getProductImg()).into(holder.itemProductImg);
        holder.itemName.setText(data.get(position).getName());
        holder.itemNewPrice.setText("￥"+(double)Math.round(data.get(position).getNewPrice()*100)/100);
        holder.itemOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder.itemOldPrice.setText((double)Math.round(data.get(position).getOldPrice()*100)/100+"");
        holder.itemDiscount.setText((double)Math.round(data.get(position).getDiscount()*100)/100+"折");
        holder.itemSaleTotal.setText("已售"+data.get(position).getSaleTotal()+"件");
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.item_ProductImg)
        ImageView itemProductImg;
        @BindView(R.id.item_Name)
        TextView itemName;
        @BindView(R.id.item_NewPrice)
        TextView itemNewPrice;
        @BindView(R.id.item_OldPrice)
        TextView itemOldPrice;
        @BindView(R.id.item_Discount)
        TextView itemDiscount;
        @BindView(R.id.item_SaleTotal)
        TextView itemSaleTotal;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener!=null){
                listener.onItemClick(v,getAdapterPosition());
            }
        }
    }
}
